package com.zl.zlbaseproject.model.net.engine;


import com.zl.zlbaseproject.model.net.bean.BaseNetBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 郑磊 on 2018/6/2.
 *
 * 用于管理回调在哪个线程 及 bean的转化
 */

public class TransformHelper{



    public static <T>ObservableTransformer<BaseNetBean,BaseNetBean<T>> transformOnMain(final Class<T> clazz) {
        return new ObservableTransformer<BaseNetBean, BaseNetBean<T>>() {
            @Override
            public ObservableSource<BaseNetBean<T>> apply(final Observable<BaseNetBean> upstream) {
                return upstream
                        .map(new Function<BaseNetBean, BaseNetBean<T>>() {
                            BaseNetBean<T> bean;

                            @Override
                            public BaseNetBean<T> apply(BaseNetBean baseNetBean) throws Exception {
                                if(baseNetBean.body!=null){
                                    bean =new BaseNetBean();
                                    String bodyString = GsonHelper.json2String(baseNetBean.body);
                                    bean.body = GsonHelper.string2Json(bodyString,clazz);
                                }
                                return bean;
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


    /**
     * 回调在子线程处理
     * @param clazz
     * @param <T>
     * @return
     */

    public static <T>ObservableTransformer<BaseNetBean,BaseNetBean<List<T>>> transformListOnIo(final Class<T> clazz) {
        return new ObservableTransformer<BaseNetBean, BaseNetBean<List<T>>>() {
            @Override
            public ObservableSource<BaseNetBean<List<T>>> apply(final Observable<BaseNetBean> upstream) {
                return upstream
                        .map(new Function<BaseNetBean, BaseNetBean<List<T>>>() {
                            BaseNetBean<List<T>> bean;

                            @Override
                            public BaseNetBean<List<T>> apply(BaseNetBean baseNetBean) throws Exception {
                                if(baseNetBean.body!=null){
                                    bean =new BaseNetBean();
                                    String bodyString = GsonHelper.json2String(baseNetBean.body);
                                    bean.body = GsonHelper.string2List(bodyString,clazz);
                                }
                                return bean;
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(Schedulers.io());
            }
        };
    }


    /**
     * 回调在子线程处理
     * @param clazz
     * @param <T>
     * @return
     */

    public static <T>ObservableTransformer<BaseNetBean,BaseNetBean<T>> transformOnIo(final Class<T> clazz) {
        return new ObservableTransformer<BaseNetBean, BaseNetBean<T>>() {
            @Override
            public ObservableSource<BaseNetBean<T>> apply(final Observable<BaseNetBean> upstream) {
                return upstream
                        .map(new Function<BaseNetBean, BaseNetBean<T>>() {
                            BaseNetBean<T> bean;

                            @Override
                            public BaseNetBean<T> apply(BaseNetBean baseNetBean) throws Exception {
                                if(baseNetBean.body!=null){
                                    bean =new BaseNetBean();
                                    String bodyString = GsonHelper.json2String(baseNetBean.body);
                                    bean.body = GsonHelper.string2Json(bodyString,clazz);
                                }
                                return bean;
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(Schedulers.io());
            }
        };
    }



    public static <T>ObservableTransformer<BaseNetBean,BaseNetBean<List<T>>> transformListOnMain(final Class<T> clazz) {
        return new ObservableTransformer<BaseNetBean, BaseNetBean<List<T>>>() {
            @Override
            public ObservableSource<BaseNetBean<List<T>>> apply(final Observable<BaseNetBean> upstream) {
                return upstream
                        .map(new Function<BaseNetBean, BaseNetBean<List<T>>>() {
                            BaseNetBean<List<T>> bean;

                            @Override
                            public BaseNetBean<List<T>> apply(BaseNetBean baseNetBean) throws Exception {
                                if(baseNetBean.body!=null){
                                    bean =new BaseNetBean();
                                    String bodyString = GsonHelper.json2String(baseNetBean.body);
                                    bean.body = GsonHelper.string2List(bodyString,clazz);
                                }
                                return bean;
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
}
